<template>
  <div class="page">

    <!-- 搜索区域 开始 -->
    <div class="el_card_box">
      <el-form :inline="true" size="small" :model="formInline" class="search_form">
        <el-form-item label="预约时间：">
          <el-date-picker v-model="formInline.orderTime" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" value-format="yyyy-MM-dd">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="预约城市：">
          <city-select v-model="formInline.cities"></city-select>
        </el-form-item>
        <el-form-item label="预约门诊：">
          <hospital-select v-model="formInline.hospitals"></hospital-select>
        </el-form-item>
        <el-form-item label="责任人：">
          <user-select v-model="formInline.principals"></user-select>
        </el-form-item>
        <el-form-item label="关键词：">
          <el-input v-model="formInline.term" placeholder="请输入客户电话/姓名"></el-input>
        </el-form-item>
        <el-form-item label="是否欠费：">
          <el-select v-model="formInline.debtFlag" class="a_select" placeholder="请选择" clearable>
            <el-option label="否" value="0"></el-option>
            <el-option label="是" value="1"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div class="search_btn_box">
        <el-button type="primary" size="small" @click="getList(1)">筛选</el-button>
        <el-button type="default" size="small" @click="rest">清空筛选</el-button>
      </div>
    </div>
    <!-- 搜索区域 end -->

    <!-- 表格 开始 -->
    <div class="table_box">
      <!-- 操作区域 开始 -->
      <div class="action_box">
        <el-button type="primary" size="small" :loading="eloading" @click="downloadExcel()">批量导出</el-button>
        <auth-button uid="3080402" type="default" size="small" :loading="eloading" @click.native="downloadExcel(1)">批量导出非脱敏记录</auth-button>
      </div>
      <!-- 操作区域 end -->

      <el-table ref="elTable" class="el_table" size="small" show-summary :summary-method="getSummaries" :data="tableData">
        <el-table-column prop="departmentName" label="客户信息" width="150">
          <template slot-scope="{row}">
            <tab-user :user="userFormatter(row)" v-if="row"></tab-user>
          </template>
        </el-table-column>
        <el-table-column prop="hospitalName" label="预约门诊" />
        <el-table-column prop="appointmentTime" label="预约时间" />
        <el-table-column prop="principal" label="责任人" />
        <el-table-column prop="dealAmount" label="成交金额" />
        <el-table-column prop="collectMoney" label="收款金额" />
        <el-table-column prop="debtMoney" label="欠费" />
        <el-table-column prop="settlableAmount" label="可结算金额" />
        <el-table-column prop="alreadySettledAmount" label="已结算金额" />
        <el-table-column prop="debtSettleAmount" label="欠费结算金额" />
        <el-table-column prop="address" label="操作" width="180">
          <template slot-scope="{row}">
            <el-link type="primary" class="el_link" @click="lookDetail(row)">结算明细</el-link>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination_list">
        <el-pagination background layout="sizes, total,prev, pager, next, jumper" :total="pageTotal" :page-size="pageSize" :page-sizes="pageSizes" :current-page.sync="pageNumber" @current-change="getList" @size-change="changePageSize" />
      </div>
    </div>
    <!-- 表格 end -->

  </div>
</template>

<script>
import {
  GET_SETTLEMENTOVERVIEW_SEARCH,
  GET_SETTLEMENTOVERVIEW_EXPORT,
} from "@/http/finance";
import { showModal, showToast, deepClone } from "@/common/util";
export default {
  data() {
    return {
      formInline: {},
      tableData: [],
      pageTotal: 0,
      pageNumber: 1,
      pageSizes: [10, 20, 50, 100],
      pageSize: 20,
      loading: false,
      eloading: false,

      summary:{}
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    // 格式化处理用户信息
    userFormatter(row) {
      let o = deepClone(row);
      o.id = row.clueId
      o.customerName = o.name;
      o.customerPhone = o.phone;
      o.pageType = this.id;
      return o;
    },

    /** 获取用户列表 */
    getList(current) {
      let _this = this;
      let oParams = _this.getParams();
      oParams.pageNumber = current || _this.pageNumber;
      console.log(oParams);
      _this.loading = true;
      GET_SETTLEMENTOVERVIEW_SEARCH(oParams).then((res) => {
        console.log(res);
        _this.loading = false;
        let obj = res.data || {};
        _this.tableData = obj.list || [];
        _this.pageTotal = obj.totalElements;
        _this.summary = res.summary || {}
      });
    },
    /** 每页展示 */
    changePageSize(e) {
      this.pageSize = e;
      this.getList();
    },

    /** 获取参数 */
    getParams() {
      let o = JSON.parse(JSON.stringify(this.formInline));
      let n = o.orderTime || [];
      if (n && n.length > 0) {
        o.orderStartTime = n[0];
        o.orderEndTime = n[1];
      }
      o.pageSize = this.pageSize;
      delete o.orderTime;
      return o;
    },

    /** 重置 */
    rest() {
      this.formInline = {};
      this.pageNumber = 1;
      this.getList();
    },

    // 导出excel
    downloadExcel(type) {
      let _this = this;
      let o = deepClone(_this.formInline);
      let m = Object.keys(o);
      if (m.length > 0) {
        showModal({
          message: "确定是否导出筛选条件下面所有结算总览列表?",
          showCancelButton: true,
          type: "warning",
          success: function () {
            _this.dowloadExcelFile(type);
          },
        });
        return false;
      }

      _this.dowloadExcelFile(type);
    },

    // 下载excel
    dowloadExcelFile(type) {
      let _this = this;
      let oParams = _this.getParams();
      let options = {};
      if(type){
        oParams.desensitize = type;
      }
      delete oParams.pageNumber;
      delete oParams.pageSize;

      options.method = "GET";
      options.data = oParams;
      options.url = GET_SETTLEMENTOVERVIEW_EXPORT;
      options.fileName = "结算总览列表.xlsx";
      _this.eloading = true;
      _this.$exportExcel(options).then((res) => {
        _this.eloading = false;
      });
    },

    // 查看明细
    lookDetail(row) {
      this.$router.push({ name: "financeDetails", query: { id: row.id } });
    },

    // 获取统计数据
    getSummaries(param) {
      const { columns, data } = param;
      const sums = [];
      let list = ['dealAmount', 'collectMoney', 'debtMoney', 'settlableAmount','alreadySettledAmount','debtSettleAmount'];
      let o = this.summary
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '合计';
          return;
        }
        const values = data.map(item => Number(item[column.property]));
        let idx = list.indexOf(column.property)
        if (idx >= 0) {
          sums[index] = o[column.property];
        } else {
          sums[index] = '';
        }
      });
      return sums;
    }

  },
};
</script>

<style lang="scss" scoped>
</style>
